from loguru import logger
from sqlalchemy.orm import Session

from app.api import deps
from app.core.celery_app import celery_app
from app.core.constant_code import ConstantItem
from app.models.instrument import Instrument
from app.spider.ashare.baostock_bar_spider import BaostockBarSpider
from app.spider.ashare.sina_realtime_bar_spider import SinaRealtimeBarSpider
from app.spider.ashare.tdx_instrument_spider import TdxInstrumentSpider
from app.spider.ashare.tushare_calendar_spider import TushareCalendarSpider
from app.spider.ashare.tushare_instrument_spider import TushareInstrumentSpider
from app.spider.binance.binance_bar_spider import BinanceBarSpider


@celery_app.task(acks_late=True)
def calendarSpider():
    try:
        logger.info("TdxSpiderTask -> calendarSpider......start")
        db: Session = next(deps.getDb())
        spider = TushareCalendarSpider(db)
        spider.crawlCalendar()
        logger.info("TdxSpiderTask -> calendarSpider......end")
    except Exception as ex:
        logger.exception("TdxSpiderTask -> calendarSpider -> 交易所时间爬取失败")
    finally:
        logger.info("TdxSpiderTask -> calendarSpider......end")


@celery_app.task(acks_late=True)
def instrumentSpider():
    try:
        logger.info("TdxSpiderTask -> instrumentSpider......start")
        db: Session = next(deps.getDb())
        spider = TushareInstrumentSpider(db)
        spider.crawlInstrumentBasic()
        logger.info("TdxSpiderTask -> instrumentSpider......end")
    except Exception as ex:
        logger.exception("TdxSpiderTask -> instrumentSpider -> 交易对爬取失败")
    finally:
        logger.info("TdxSpiderTask -> instrumentSpider......end")


@celery_app.task(acks_late=True)
def barSpider():
    try:
        logger.info("TdxSpiderTask -> barSpider......start")
        db: Session = next(deps.getDb())
        instruments = db.query(Instrument).filter(Instrument.marketType == ConstantItem.MARKET_STOCKS,
                                                  Instrument.countryCode == 'CN').all()
        spider = BaostockBarSpider(db, instruments)
        spider.crawlDay()
        logger.info("TdxSpiderTask -> barSpider......day end")
        spider.crawl1hour()
        logger.info("TdxSpiderTask -> barSpider...... 1hour end")
    except Exception as ex:
        logger.exception("TdxSpiderTask -> barSpider -> K线爬取失败")
    finally:
        logger.info("TdxSpiderTask -> barSpider......end")


def calendarSpiderJob():
    try:
        logger.info("TdxSpiderTask -> calendarSpiderJob......start")
        db: Session = next(deps.getDb())
        spider = TushareCalendarSpider(db)
        spider.crawlCalendar()
        logger.info("TdxSpiderTask -> calendarSpiderJob......end")
    except Exception as ex:
        logger.exception("TdxSpiderTask -> calendarSpiderJob -> 交易所时间爬取失败")
    finally:
        logger.info("TdxSpiderTask -> calendarSpiderJob......end")


def instrumentSpiderJob():
    try:
        logger.info("TdxSpiderTask -> instrumentSpiderJob......start")
        db: Session = next(deps.getDb())
        spider = TushareInstrumentSpider(db)
        spider.crawlInstrumentBasic()
        logger.info("TdxSpiderTask -> instrumentSpiderJob......end")
    except Exception as ex:
        logger.exception("TdxSpiderTask -> instrumentSpider -> 交易对爬取失败")
    finally:
        logger.info("TdxSpiderTask -> instrumentSpiderJob......end")


def barSpiderDayJob():
    try:
        logger.info("TdxSpiderTask -> barSpiderDayJob......start")
        db: Session = next(deps.getDb())
        instruments = db.query(Instrument).filter(Instrument.marketType == ConstantItem.MARKET_STOCKS,
                                                  Instrument.countryCode == 'CN').all()
        spider = BaostockBarSpider(db, instruments)
        spider.crawlDay()
        logger.info("BaostockBarSpiderTask -> barSpiderDayJob......day end")
    except Exception as ex:
        logger.exception("BaostockBarSpiderTask -> barSpiderDayJob -> K线爬取失败")
    finally:
        logger.info("BaostockBarSpiderTask -> barSpiderDayJob......end")

def barSpiderHourJob():
    try:
        logger.info("BaostockBarSpiderTask -> barSpiderHourJob......start")
        db: Session = next(deps.getDb())
        instruments = db.query(Instrument).filter(Instrument.marketType == ConstantItem.MARKET_STOCKS,
                                                  Instrument.countryCode == 'CN').all()
        spider = BaostockBarSpider(db, instruments)
        spider.crawl1hour()
        logger.info("BaostockBarSpiderTask -> barSpiderHourJob...... 1hour end")
    except Exception as ex:
        logger.exception("BaostockBarSpiderTask -> barSpiderHourJob -> K线爬取失败")
    finally:
        logger.info("BaostockBarSpiderTask -> barSpiderHourJob......end")

def realtimeSpiderDayJob():
    try:
        logger.info("TdxSpiderTask -> realtimeSpiderDayJob......start")
        db: Session = next(deps.getDb())
        instruments = db.query(Instrument).filter(Instrument.marketType == ConstantItem.MARKET_STOCKS,
                                                  Instrument.countryCode == 'CN').all()
        spider = SinaRealtimeBarSpider(db, instruments)
        spider.crawlDay()
        logger.info("TdxSpiderTask -> realtimeSpiderDayJob......day end")
    except Exception as ex:
        logger.exception("TdxSpiderTask -> realtimeSpiderDayJob -> K线爬取失败")
    finally:
        logger.info("TdxSpiderTask -> realtimeSpiderDayJob......end")